package main

import (
	log "github.com/sirupsen/logrus"
	"io"
	"os"
	"runtime"
	"thomas-mesh/discovery"
	"thomas-mesh/http"
	"thomas-mesh/ssh"
	"thomas-mesh/uc"
	"time"
)

const (
	httpAddr  = "0.0.0.0:9333"
	ucAddr    = "0.0.0.0:18002"
	trpcAddr  = "0.0.0.0:9335"
	websocket = "0.0.0.0:9336"
)

var (
	blockStop = make(chan bool)
)

func init() {
	initDebugPprof()
	// 初始化日志组件
	initLog()
}

func main() {
	go discovery.RegisterAgentFixTime(30 * time.Second)
	go uc.Connect(ucAddr)
	go ssh.WebSocket(websocket)
	go http.Server(httpAddr)
	<-blockStop
}

func initDebugPprof() {
	// 开启锁采集
	runtime.SetMutexProfileFraction(1)
	// 开启阻塞采集
	runtime.SetBlockProfileRate(1)
	log.Info("---Debug Pprof init---")
}

func initLog() {
	file, err := os.OpenFile("thomas-mesh.log", os.O_CREATE|os.O_WRONLY, 0666)
	if err == nil {
		mw := io.MultiWriter(os.Stdout, file)
		log.SetOutput(mw)
		log.Infof("---logfile init :%s---", "thomas-mesh.log")
	} else {
		log.Error("Failed to log to file")
	}
}
